#!/home/abc/anaconda3/bin/python
import pygame
import myplane
import enemy
import supply
import bullet
from sys import exit
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((480,700))
bg=pygame.image.load('images/background.png').convert()
screen.blit(bg,(0,0))
again=pygame.image.load('images/again.png').convert_alpha()
again_rect=again.get_rect()
again_rect.x,again_rect.y=(90,330)
gameover=pygame.image.load('images/gameover.png').convert_alpha()
gameover_rect=gameover.get_rect()
gameover_rect.x,gameover_rect.y=(90,390)
font=pygame.font.Font('font/font.ttf',32)

def main():
    clock=pygame.time.Clock()
    plane=myplane.Myplane(480,700)

    bullet_list=[bullet.Bullet(plane.rect) for x in range(4)]
    bullet_index=0
    delay=0
    enemys_group=pygame.sprite.Group([enemy.Enemy(480,700) for x in range(10)])
    score=0

    bomb=supply.Bomb_supply(480,700)
    BOMBSUPPLYEVN=USEREVENT+1
    pygame.time.set_timer(BOMBSUPPLYEVN,30*1000)


    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                exit()
            elif event.type==BOMBSUPPLYEVN:
            	bomb.active=True
            	print("fsdfsd")
        #屏幕
        screen.blit(bg,(0,0))
        
        #飞机
        plane.move()
        plane.collide(enemys_group)
        plane.draw(screen)
        
        #子弹 
        if not delay%12:
            bullet_list[bullet_index%4].reset(plane.rect)
            bullet_index+=1

        for b in bullet_list:
            b.move()
            b.collide(enemys_group)
            b.draw(screen)
        
        #敌机
        for e in enemys_group:
            e.move()
            e.draw(screen)
            if e.score:
            	score+=e.score
            	e.score=0

        bomb.move()
        bomb.collide(plane)
        bomb.draw(screen)

        #打印份数
        screen.blit(font.render("score:"+str(score),True,(0,0,0)),(20,5))

        if plane.life_num==0:
            break
        delay+=1
        pygame.display.flip()
        clock.tick(30)

def game():
    while True:
        main()
        #退出游戏
        screen.blit(bg,(0,0))
        screen.blit(again,again_rect)
        screen.blit(gameover,gameover_rect)
        pygame.display.flip()
        while True:
        	for event in pygame.event.get():
        		if event.type==QUIT:
        			exit()
        	if pygame.mouse.get_pressed()[0]:
        		print(pygame.mouse.get_pressed())
        		mx,my=pygame.mouse.get_pos()
        		print(mx,my)
        		if again_rect.collidepoint(mx,my):
        			break
        		if gameover_rect.collidepoint(mx,my):
        			pygame.quit()
        			exit()

        

game()

